package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserMapper;
import com.itheima.domain.User;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ServiceDemo {
    private SqlSession session;

    @Before
    public void init() throws IOException {
        //1. 加载核心配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2. 创建工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3. 获取session
        session = factory.openSession(true);
    }

    @After
    public void release() {
        session.close();
    }

    @Test
    public void login() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.login("路飞","4564");
        System.out.println(user);
    }

    @Test
    public void findByIds() {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(2);
        ids.add(4);
        ids.add(6);
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findByIds(ids);
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void findAll() {
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void save() {
        User user = new User();

        user.setUsername("dd");
        user.setPassword("123");
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.save(user);
    }
    @Test
    public void delete(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.delete(1);
    }

    @Test
    public void findByName(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findByName("%今%");
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void save1(){
        User user = new User();
        user.setId(1);
        user.setUsername("菲菲");
        user.setPassword("12354");

        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.save1(user);
    }
    @Test
    public void findCount(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        int count = mapper.findCount();
        System.out.println(count);
    }
    @Test
    public void update(){
        User user = new User();
        user.setId(1);
        user.setUsername("xdd");
        user.setPassword("14562");
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.update(user);
    }
    @Test
    public void save2(){
        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword("4679");
        System.out.println(user);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.save2(user);
        System.out.println(user);

    }
    @Test
    public void dt(){
        User user = new User();
        user.setUsername("%赵%");
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.dt(user);
        for (User user1 : users) {
            System.out.println(user1);
        }
    }
    @Test
    public void page(){
        PageHelper.startPage(2,3);
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.page();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void findByIds2(){
        Integer[] arr = {1,2,4,6};
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findByIds2(arr);
        for (User user : users) {
            System.out.println(user);
        }
    }
}


